package com.yycx.module.bus.provider.handler.haiying.tasktodo.saleman;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import com.yycx.module.user.client.entity.AppPosition;
import com.yycx.module.user.provider.service.AppPositionService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务员审核-我的待办
 *
 * @author zyf
 */
@Component
public class SaleManTaskTab1ListHandler implements FlowPageInterceptor<TaskResponse> {
    @Resource
    private BusSalemanService salemanService;
    @Resource
    private BusDeptService deptService;
    @Resource
    private AppPositionService positionService;
    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String saleManName = params.get("saleManName");
        if (FlymeUtils.isNotEmpty(saleManName)) {
            taskQuery.processVariableValueLike("saleManName", saleManName);
        }
        //关联租户ID
        taskQuery.taskTenantId("flp").orderByTaskCreateTime().desc();
        //关联流程key
        taskQuery.processDefinitionKey("process_saleman_flp");
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = taskResponse.getResult();
                String saleManId = taskResponse.getBusinessKey();
                //查询订单信息
                if (FlymeUtils.isNotEmpty(saleManId)) {
                    EntityMap saleManMap = salemanService.findById(saleManId);
                    Long deptId=saleManMap.getLong("deptId");
                    if(FlymeUtils.isNotEmpty(deptId)){
                        BusDept dept=deptService.getById(deptId);
                        map.put("deptName",dept.getDeptName());
                    }
                    Long positionId=saleManMap.getLong("positionId");
                    if(FlymeUtils.isNotEmpty(positionId)){
                        AppPosition position=positionService.getById(positionId);
                        map.put("positionName",position.getPositionName());
                    }
                    if (FlymeUtils.isNotEmpty(saleManMap)) {
                        map.putAll(saleManMap);
                    }
                    list.add(map);
                }
            }
        }
        return list;
    }
}
